package com.building18.controller;

import com.building18.handler.MyAssert;
import com.building18.pojo.Owner;
import com.building18.pojo.MyPageData;
import com.building18.pojo.Result;
import com.building18.pojo.enums.BuildingCode;
import com.building18.pojo.enums.Code;
import com.building18.pojo.vo.OwnerVO;
import com.building18.service.OwnerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/owner")
@Tag(name = "业主管理", description = "业主信息的增删改查接口")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @GetMapping("/{pageNum}/{pageSize}")
    @Operation(summary = "查询所有业主信息（分页）")
    public Result<MyPageData> selectOwner(@PathVariable int pageNum, @PathVariable int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Owner> owners = ownerService.selectOwner();
        PageInfo<Owner> ownerPageInfo = new PageInfo<>(owners);
        MyPageData myPageData = new MyPageData(ownerPageInfo.getTotal(), ownerPageInfo.getList());
        return Result.ok(myPageData, Code.SELECT_SUCCESS.getMessage());
    }

    @PostMapping("/insertOwner")
    @Operation(summary = "添加业主")
    public Result<Integer> addOwner(@RequestBody Owner owner) {
        Integer i = ownerService.addOwner(owner);
        MyAssert.error(i<=0, Code.INSERT_FAIL.getMessage());
        return Result.ok(i, Code.INSERT_SUCCESS.getMessage());
    }

    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "根据id删除业主")
    public Result<Integer> deleteOwnerById(@PathVariable("id") Integer id) {
        Integer i = ownerService.deleteOwnerById(id);
        MyAssert.error(i<=0, Code.DELETE_FAIL.getMessage());
        return Result.ok(i, Code.DELETE_SUCCESS.getMessage());
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "根据ids删除业主")
    public Result<Integer> deleteOwnerByIds(@RequestBody Integer[] ids) {
        Integer i = ownerService.deleteOwnerByIds(ids);
        MyAssert.error(i<=0, Code.DELETE_FAIL.getMessage());
        return Result.ok(i, Code.DELETE_SUCCESS.getMessage());
    }

    @PostMapping("/selectOwnerByName/{name}")
    @Operation(summary = "根据业主名称查询")
    public Result<List<Owner>> selectOwnerByName(@PathVariable String name) {
        List<Owner> owners = ownerService.selectOwnerByName(name);
        return Result.ok(owners, Code.SELECT_SUCCESS.getMessage());
    }

    @PutMapping("/updateOwner")
    @Operation(summary = "修改业主信息")
    public Result<Integer> updateOwner(@RequestBody Owner owner) {
        Integer i = ownerService.updateOwner(owner);
        return Result.ok(i, Code.UPDATE_SUCCESS.getMessage());
    }

    @GetMapping("/getOwnerNameAndId")
    @Operation(summary ="查询所有业主编号和id")
    public Result<List<OwnerVO>> getOwnerNoAndId() {
        List<OwnerVO> ownerNameAndId = ownerService.getOwnerNameAndId();
        return Result.ok(ownerNameAndId, Code.SELECT_SUCCESS.getMessage());
    }

    @GetMapping("/getOwnerById/{id}")
    @Operation(summary ="根据id查询业主信息")
    public Result<OwnerVO> getOwnerById(@PathVariable Integer id) {
        OwnerVO ownerVO = ownerService.getOwnerById(id);
        if (ownerVO == null) {
            return Result.fail(Code.SELECT_FAIL.getMessage());
        }else {
            return Result.ok(ownerVO, Code.SELECT_SUCCESS.getMessage());
        }
    }
}